﻿/*
 * 图片尺寸和压缩关系
 * https://www.cnblogs.com/cyct/p/11325900.html
 **/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class TestCutTexture : EditorWindow
{
    [MenuItem("Window/贴图剪裁")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(TestCutTexture)).Show();
    }
    Rect rect;
    int currDragTo;
    string path;
    Texture2D mymeScale;
    void OnGUI()
    {
        string cutString = "0";
        string sizeString = "0";
        if (GUILayout.Button("Cut" ))
        {
            cutString = DoCut().ToString();
        }
        //Vector2 texel = mymeScale.texelSize;
        //string sizeString = (texel.x * texel.y).ToString();
        if (mymeScale != null)
        {
            int formatByteCount = GetFormatByte(mymeScale.format);
            sizeString = string.Format("{3} ={0}x{1} - {2}byte", mymeScale.width, mymeScale.height,
                //mymeScale.format.ToString(),
                formatByteCount,
                (mymeScale.width * mymeScale.height * formatByteCount / 1024f).ToString());
        }
        GUILayout.Label("src:" + sizeString);
        GUILayout.Label("cut:");
        OnPropertiesGUI();
        #region Drag & Drop
        rect = EditorGUILayout.GetControlRect(GUILayout.Width(400), GUILayout.Height(280));
        //EditorGUI.DrawRect(rect, Color.yellow);

        Rect rect2 = new Rect(400 + 10, rect.y, 400, 280);
        //EditorGUI.DrawPreviewTexture(rect,);
        //2017后EventType.DragUpdated不能小写咯
        if (Event.current.type == EventType.DragUpdated)
        {
            if (rect.Contains(Event.current.mousePosition))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
                {
                    path = DragAndDrop.paths[0];
                    currDragTo = 1;
                }
                Debug.Log(Event.current.type);
            }
            else if (rect2.Contains(Event.current.mousePosition))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
                if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
                {
                    path = DragAndDrop.paths[0];
                    currDragTo = 2;
                }


            }

        }
        else if (Event.current.type == EventType.DragExited)//在dragPerform之后还会触发一次，只能实现去除 mask 了
        {
            path = "";
            Debug.Log(Event.current.type);
        }
        else if (Event.current.type == EventType.DragPerform)
        {
            if (currDragTo == 2)
            {
                //LoadToCache(path, (tex) => {


                //    mymeScale2 = TextureUtils.TextureToTexture2D(tex);
                //    //拖拉2事件    
                //    toggleOne = false;
                //    toggleSecond = true;
                //    m_isRenderDirty = true;
                //    UpdateSectionEvent();

                //});
            }
            else if (currDragTo == 1)
            {
                LoadToCache(path, (tex) =>
                {

                    mymeScale = TextureUtils.TextureToTexture2D(tex);
                   
                });
            }
            Debug.LogWarning("drag 生效");
        }
        else if (Event.current.type == EventType.MouseUp)
        {
            //还会触发2次。。。。
            Debug.Log("mouse up");
        }


        #endregion

        if(mymeScale!=null)
            EditorGUI.DrawPreviewTexture(rect, mymeScale, null, ScaleMode.ScaleToFit);
    }

    void OnPropertiesGUI()
    {
        OnPropGUI("currentTextureMemory", Texture.currentTextureMemory);
        OnPropGUI("totalTextureMemory", Texture.totalTextureMemory);
        OnPropGUI("desiredTextureMemory", Texture.desiredTextureMemory);
    }
    void OnPropGUI(string title,ulong v1,ulong v2=0) {
        GUILayout.BeginHorizontal();
        GUILayout.Label(title);
        GUILayout.Label(v1.ToString());
        //GUILayout.Label(v2.ToString());
        GUILayout.EndHorizontal();
    }
    int GetFormatByte(TextureFormat format)
    {
        switch (format)
        {
            case TextureFormat.ARGB32:
                return 4;
                break;
            case TextureFormat.RGB24:
                return 3;
            default:
                return 1;
                break;

        }
    }
    void LoadToCache(string path, Action<Texture> cb)
    {
        EditorCoroutineRunner.StartCoroutine(TextureUtils.LoadTexture(path, cb));
    }
    int DoCut() {
        return 0;
    }
}
